package com.ev.k3cloudBusiness.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ev.framework.annotation.EvApiByToken;
import com.ev.framework.utils.R;
import com.ev.k3cloud.vo.IdsVo;
import com.ev.k3cloudBusiness.service.K3SalOutStockService;
import com.ev.k3cloudBusiness.vo.K3SalOutStockUpdateQtyVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Api(value = "/", tags = "金蝶K3Cloud销售出库单")
@RestController
public class K3SalOutStockController {

    @Autowired
    K3SalOutStockService k3SalOutStockService;

    @EvApiByToken(value = "/apis/salOutStock/pushOutDetailByIds", method = RequestMethod.POST, apiTitle = "根据内码下推出运明细单")
    @ApiOperation("根据内码下推出运明细单")
    public R getBySourceBillNo(@RequestBody @Param("ids") IdsVo ids) {
        try {
            Map<String, Object> resultInfo = new HashMap<>();
            String result = k3SalOutStockService.pushOutDetailByIds(ids);
            JSONObject resultObj = JSON.parseObject(result);
            if( resultObj.getJSONObject("Result").getJSONObject("ResponseStatus").getBooleanValue("IsSuccess") ) {
                resultInfo.put("data", resultObj.getJSONObject("Result").getJSONArray("SuccessEntitys").get(0));
                return R.ok(resultInfo);
            } else {
                return R.error(resultObj.getJSONObject("Result").getJSONObject("ResponseStatus").getJSONArray("Errors").toString());
            }
        }
        catch (Exception ex) {
            return R.error(ex.getMessage());
        }
    }

    @EvApiByToken(value = "/apis/salOutStock/pushOutDetailByNumbers", method = RequestMethod.POST, apiTitle = "根据内码下推出运明细单")
    @ApiOperation("根据单据编号下推出运明细单")
    public R pushOutDetailByNumbers(@RequestBody @Param("billNumbers") String[] billNumbers) {
        try {
            String result = k3SalOutStockService.pushOutDetailByNumbers(billNumbers);
            JSONObject resultObj = JSON.parseObject(result);
            if( resultObj.getJSONObject("Result").getJSONObject("ResponseStatus").getBooleanValue("IsSuccess") ) {
                return R.ok();
            } else {
                return R.error(resultObj.getJSONObject("Result").getJSONObject("ResponseStatus").getJSONArray("Errors").toString());
            }
        }
        catch (Exception ex) {
            return R.error(ex.getMessage());
        }
    }

    @EvApiByToken(value = "/apis/salOutStock/updateBillInfo", method = RequestMethod.POST, apiTitle = "更新单据信息")
    @ApiOperation("更新单据信息")
    public R updateBillInfo(@RequestBody @Param("param") K3SalOutStockUpdateQtyVo param) {
        try {
            String result = k3SalOutStockService.updateBillInfo(param);
            JSONObject resultObj = JSON.parseObject(result);
            if( resultObj.getJSONObject("Result").getJSONObject("ResponseStatus").getBooleanValue("IsSuccess") ) {
                return R.ok();
            } else {
                return R.error(resultObj.getJSONObject("Result").getJSONObject("ResponseStatus").getJSONArray("Errors").toString());
            }
        }
        catch (Exception ex) {
            return R.error(ex.getMessage());
        }
    }

    @EvApiByToken(value = "/apis/salOutStock/validateReferenceByBillNo", method = RequestMethod.GET, apiTitle = "根据单据编号验证销售出库单是否被引用")
    @ApiOperation("根据单据编号验证销售出库单是否被引用")
    public R updateBillInfo(@RequestParam @Param("billNO") String billNo) {
        try {
            Map<String, Object> resultMap = new HashMap<>();
            if (k3SalOutStockService.validateReferenceByBillNo(billNo)) {
                resultMap.put("data", true);
            } else {
                resultMap.put("data", false);
            }
            return R.ok(resultMap);
        }
        catch (Exception ex) {
            return R.error(ex.getMessage());
        }
    }

    @EvApiByToken(value = "/apis/salOutStock/getBillInfoByNo", method = RequestMethod.GET, apiTitle = "根据单据编号获取单据信息")
    @ApiOperation("根据单据编号获取单据信息")
    public R getBillInfoByNo(@RequestParam @Param("billNO") String billNo) {
        try {
            Map<String, Object> resultMap = new HashMap<>();
            K3SalOutStockUpdateQtyVo billInfo = k3SalOutStockService.getBillInfoByNo(billNo);
            resultMap.put("data", billInfo);
            return R.ok(resultMap);
        }
        catch (Exception ex) {
            return R.error(ex.getMessage());
        }
    }

}
